package com.zelic.zwrite;

import java.io.*;
import android.app.*;
import android.os.Bundle;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import com.zelic.znn.Network;
import java.util.*;

public class ZWriteActivity extends Activity {

	WriteView writeView;
	Button getImage;
	TextView result;
	Network network;
	Activity self;

	private String[] charArray = new String[] { "A", "Ă", "Â", "B", "C", "D",
			"Đ", "E", "Ê", "F", "G", "H", "K", "I", "J", "L", "M", "N", "O",
			"Ô", "Ơ", "P", "Q", "R", "S", "T", "U", "Ư", "V", "W", "X", "Y",
			"Z", "a", "ă", "â", "b", "c", "d", "đ", "e", "ê", "f", "g", "h",
			"k", "i", "j", "l", "m", "n", "o", "ô", "ơ", "p", "q", "r", "s",
			"t", "u", "ư", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4",
			"5", "6", "7", "8", "9" };
	private List<String> charset;

	private void init() {
		charset = Arrays.asList(charArray);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		try {
			network = Network.importNetworkFromFile(new BufferedInputStream(
					getResources().openRawResource(R.raw.az2c)));

			setContentView(R.layout.main);
			init();
			writeView = (WriteView) findViewById(R.id.writeView);
			self = this;

			getImage = (Button) findViewById(R.id.btnGetImage);
			result = (TextView) findViewById(R.id.tvResult);
			getImage.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					double[] input = writeView.getStrokesVector();
					double[] output = network.use(input);
					int pos = Util.getPositionFromResult(output);

					result.setText(charset.get(pos));
					writeView.resetView();
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}